package cn.itcast.bos.web.action.take_delivery;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.take_delivery.Order;
import cn.itcast.bos.domain.take_delivery.WayBill;
import cn.itcast.bos.domain.utils.BaseAction;
import cn.itcast.bos.service.take_delivery.WayBillService;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class WayBillAction extends BaseAction<WayBill> {
	private static final Logger logger = Logger.getLogger(WayBillAction.class);
	@Autowired
	private WayBillService wayBillService;

	@Action(value = "waybill_save", results = { @Result(name = "success", type = "json") })
	public String saveWayBill() {
		 
		 if(model.getOrder()!=null&&(model.getOrder().getId()==null||model.getOrder().getId()==0)){
			model.setOrder(null);
		 }
		
		// 1.创建Map集合存储结果
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			// 2.如果订单保存没有出现异常说明保存成功
			wayBillService.save(model);
			result.put("success", true);
			result.put("msg", "订单保存成功");
			logger.info("订单保存成功,运单号为" + model.getWayBillNum());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			// 3.如果发生异常给出提示
			e.printStackTrace();
			result.put("success", false);
			result.put("msg", "订单保存失败");
			logger.error("订单保存失败,运单号为" + model.getWayBillNum());
		}
		// 4.将map集合压入值栈
		ServletActionContext.getContext().getValueStack().push(result);
		return SUCCESS;
	}

	@Action(value = "wayBillPageQuery", results = { @Result(name = "success", type = "json") })
	public String pageQuery() {
		// 接受分页相关参数,然后进行排序,调用Service层方法,最后将查询出的数据返回
		Pageable pageable = new PageRequest(page - 1, rows, new Sort(new Sort.Order(Sort.Direction.DESC, "id")));
		// ,调用Service层方法,
		Page<WayBill> pageData = wayBillService.findAll(model,pageable);
		// 压入值栈
		pushPageDataToValueStack(pageData);
		return SUCCESS;
	}
	@Action(value="findBywayBillNum",results={@Result(name="success",type="json")})
	public String findwayBillNum(){
		WayBill wayBill= wayBillService.findBywayBillNum(model.getWayBillNum());
		 Map<String, Object> result= new HashMap<String,Object>();
		  if(wayBill!=null){
			  //说明查询到了数据
			  result.put("success", true);
			  result.put("wayBillData", wayBill);
		  }else{
			  result.put("success", false);
		  }
		 ServletActionContext.getContext().getValueStack().push(result);
		
		return SUCCESS;
	}
}
